package com.example;
import com.example.entity.User;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        UserManager userManager = new UserManager();
        Scanner scanner = new Scanner(System.in);
        AuthenticationService authService = new AuthenticationService(userManager);

        // 初始化测试用户
        userManager.addUser(new User("admin", "admin123", "admin@example.com", UserRole.ADMIN));
        userManager.addUser(new User("user", "user123", "user@example.com", UserRole.USER));

        while (true) {
            System.out.println("\n=== 信息管理系统 ===");
            System.out.println("1. 登录");
            System.out.println("2. 注册");
            System.out.println("3. 退出");
            System.out.print("请选择操作: ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    handleLogin(scanner, authService);
                    break;
                case 2:
                    handleRegistration(scanner, userManager);
                    break;
                case 3:
                    System.out.println("感谢使用系统，再见！");
                    return;
                default:
                    System.out.println("无效选择，请重新输入。");
            }
        }
    }

    private static void handleLogin(Scanner scanner, AuthenticationService authService) {
        System.out.print("用户名: ");
        String username = scanner.nextLine();
        System.out.print("密码: ");
        String password = scanner.nextLine();

        User user = authService.authenticate(username, password);
        if (user != null) {
            System.out.println("登录成功，欢迎 " + user.getUsername() + "!");
            showUserMenu(scanner, user, authService);
        } else {
            System.out.println("登录失败，用户名或密码错误。");
        }
    }

    private static void handleRegistration(Scanner scanner, UserManager userManager) {
        System.out.print("新用户名: ");
        String username = scanner.nextLine();
        System.out.print("密码: ");
        String password = scanner.nextLine();
        System.out.print("邮箱: ");
        String email = scanner.nextLine();

        if (userManager.registerUser(username, password, email, UserRole.USER)) {
            System.out.println("注册成功！");
        } else {
            System.out.println("注册失败，用户名已存在。");
        }
    }

    private static void showUserMenu(Scanner scanner, User user, AuthenticationService authService) {
        while (true) {
            System.out.println("\n=== 用户菜单 ===");
            System.out.println("1. 查看个人信息");
            System.out.println("2. 修改密码");
            if (user.getRole() == UserRole.ADMIN) {
                System.out.println("3. 管理用户");
            }
            System.out.println("4. 退出登录");
            System.out.print("请选择操作: ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    System.out.println("用户信息:");
                    System.out.println("用户名: " + user.getUsername());
                    System.out.println("邮箱: " + user.getEmail());
                    System.out.println("角色: " + user.getRole());
                    break;
                case 2:
                    System.out.print("当前密码: ");
                    String oldPassword = scanner.nextLine();
                    System.out.print("新密码: ");
                    String newPassword = scanner.nextLine();

                    if (authService.changePassword(user, oldPassword, newPassword)) {
                        System.out.println("密码修改成功！");
                    } else {
                        System.out.println("密码修改失败，当前密码不正确。");
                    }
                    break;
                case 3:
                    if (user.getRole() == UserRole.ADMIN) {
                        showAdminMenu(scanner, (UserManager) authService.getUserRepository());
                    } else {
                        System.out.println("权限不足，此功能仅限管理员使用。");
                    }
                    break;
                case 4:
                    System.out.println("已退出登录。");
                    return;
                default:
                    System.out.println("无效选择，请重新输入。");
            }
        }
    }

    private static void showAdminMenu(Scanner scanner, UserManager userManager) {
        while (true) {
            System.out.println("\n=== 管理员菜单 ===");
            System.out.println("1. 查看所有用户");
            System.out.println("2. 删除用户");
            System.out.println("3. 返回上级菜单");
            System.out.print("请选择操作: ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    System.out.println("所有用户信息:");
                    for (User u : userManager.getAllUsers()) {
                        System.out.println("用户名: " + u.getUsername() + ", 邮箱: " + u.getEmail() + ", 角色: " + u.getRole());
                    }
                    break;
                case 2:
                    System.out.print("请输入要删除的用户名: ");
                    String username = scanner.nextLine();
                    if (userManager.deleteUser(username)) {
                        System.out.println("用户删除成功！");
                    } else {
                        System.out.println("用户删除失败，用户名不存在。");
                    }
                    break;
                case 3:
                    return;
                default:
                    System.out.println("无效选择，请重新输入。");
            }
        }
    }
}